package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginContllor {

    @Autowired
    private IEmployeeService employeeService;


    @RequestMapping("/loginEmp")
    @ResponseBody
    public JsonResult loginCheck(String username, String password, HttpSession session,HttpSession httpSession){

        JsonResult jsonResult = new JsonResult();
        try{
            Employee employee = employeeService.longinCheck(username,password);
            httpSession.setAttribute(SessionUtil.USER_SUCCESS,employee);

            List<Permission> employee1 = employeeService.userCheck(employee.getId());
            httpSession.setAttribute("USER_MESSAGE",employee1);


        }catch (Exception e){
            jsonResult.setSuccess(false);
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }


    @RequestMapping("/logout")
    public String logout(HttpSession httpSession){
        httpSession.removeAttribute("USER_SUCCESS");
        return "redirect:/login.html";
    }


}
